package edu.vt.ece3574.ninjatag;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.methods.HttpPost;
import org.clear.client.http.HttpUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

import com.facebook.android.BaseRequestListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Utility;

/****************************************************************************
 * Copyright 2012 Landon Biggs, Chris Dusold, Brad Russell, and Craig Wertz	*
 * Licensed under the Apache License, Version 2.0 (the "License");			*
 * you may not use this file except in compliance with the License.			*
 * You may obtain a copy of the License at									*
 * 																			*
 * http://www.apache.org/licenses/LICENSE-2.0								*
 * 																			*
 * Unless required by applicable law or agreed to in writing, software		*
 * distributed under the License is distributed on an "AS IS" BASIS,		*
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.	*
 * See the License for the specific language governing permissions and		*
 * limitations under the License.											*
 ****************************************************************************/

/**
 * This is an attempt to make a class that implements PlayerIf so that values
 * aren't just queried every time they are needed.
 * @author Chris Dusold
 *
 */
public class FacebookPlayer extends APlayer {
	//private Handler handler = new Handler();
    boolean running = false;
    protected static JSONArray jsonArray;
    String name = null;
    Bitmap pic = null;
    String picURL = null;
    String currentLocation = "";
    GPSIf gPS = null;
	
	public FacebookPlayer(Long uID){
		super("f" + String.valueOf(uID));
	}
	
	public FacebookPlayer(Long uID, String name, Bitmap pic) {
		super(name,pic,"f" + String.valueOf(uID));
	}

	public FacebookPlayer(Long uID, String name, Bitmap pic,
			String string2) {
		super(name,pic,"f" + String.valueOf(uID));
		this.name=name;
		this.pic=pic;
		this.currentLocation = string2;
	}

	@Override
	public GPSIf getGPSDetails() {
		// TODO Add DummyGPS implementation whenever it's made.
		return null;
	}

	@Override
	public String queryName() {
		running = true;
        String query = "select name from user where uid=" + getUID() + " order by name";
        Bundle params = new Bundle();
        params.putString("method", "fql.query");
        params.putString("query", query);
        Utility.mAsyncRunner.request(null, params,
                new BaseRequestListener() {
					
					@Override
					public void onComplete(String response, Object state) {
			            try {
			            	jsonArray = new JSONArray(response);
							name = jsonArray.getJSONObject(0).getString("name");
						} catch (JSONException e) {
					        Log.e("FacebookPlayer", e.getMessage());
							e.printStackTrace();
						}
						running=false;
						
					}
				    @Override
				    public void onFacebookError(FacebookError e, final Object state) {
				    	super.onFacebookError(e, state);
				    	running=false;
				    }

				    @Override
				    public void onFileNotFoundException(FileNotFoundException e, final Object state) {
				    	super.onFileNotFoundException(e, state);
				    	running=false;
				    }

				    @Override
				    public void onIOException(IOException e, final Object state) {
				    	super.onIOException(e, state);
				    	running=false;
				    }

				    @Override
				    public void onMalformedURLException(MalformedURLException e, final Object state) {
				    	super.onMalformedURLException(e, state);
				    	running=false;
				    }
				});
        while (running) {
        	try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        return name;
	}

	@Override
	public String queryScore() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Bitmap queryPicture() {
		running = true;
        String query = "select uid, pic_square from user where uid=" + getUID() + " order by name";
        Bundle params = new Bundle();
        params.putString("method", "fql.query");
        params.putString("query", query);
        Utility.mAsyncRunner.request(null, params,
                new BaseRequestListener() {
					
					@Override
					public void onComplete(String response, Object state) {
			            try {
			            	jsonArray = new JSONArray(response);
							pic = Utility.model.getImage(
		                            jsonArray.getJSONObject(0).getString("uid"), jsonArray.getJSONObject(0).getString("pic_square"));
						} catch (JSONException e) {
					        Log.e("FacebookPlayer", e.getMessage());
							e.printStackTrace();
						}
						running=false;
						
					}
				    @Override
				    public void onFacebookError(FacebookError e, final Object state) {
				    	super.onFacebookError(e, state);
				    	running=false;
				    }

				    @Override
				    public void onFileNotFoundException(FileNotFoundException e, final Object state) {
				    	super.onFileNotFoundException(e, state);
				    	running=false;
				    }

				    @Override
				    public void onIOException(IOException e, final Object state) {
				    	super.onIOException(e, state);
				    	running=false;
				    }

				    @Override
				    public void onMalformedURLException(MalformedURLException e, final Object state) {
				    	super.onMalformedURLException(e, state);
				    	running=false;
				    }
				});
        while (running) {
        	try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        return pic;
	}

	@Override
	public void addSaveValues(Editor editor) {
		editor.putString("Type", "Facebook");
		editor.putString("Facebook current location", currentLocation);
		
	}
	
	public String getLocation() {
		return currentLocation;
	}
	
	public void setLocation(Location loc) {
		if (gPS == null) {
			gPS = new GPSIf() {
				private Location loc;
				
				@Override
				public String getType() {
					return "Android phone";
				}
				
				@Override
				public long getTimeSinceLastCheck() {
					return 0;
				}
				
				@Override
				public String getSensitivity() {
					return null;
				}
				
				@Override
				public Location getLocation() {
					return loc;
				}
				
				@Override
				public void setLocation(Location location) {
					loc=location;
				}
			};
		}
		gPS.setLocation(loc);
		setGPSCoordinates(gPS);
	}

} //TODO: add class that handles queries, and concatenates queries together when multiple are requested in series.
